﻿#include <wiringPi.h>
#include <wiringSerial.h>

#include <iostream>
#include <unistd.h>
#include <sqlite3.h>
#include <cstring>
#include <python3.7/Python.h>
#include <thread>

#include "HumitureSensor.h"
#include "LaserSensor.h"
#include "PM25Sensor.h"
#include "mPython.h"
#include "InfraredTemperatureSensor/InfraredTemperatureSensor.h"

#include "SimDongle.h"

#include "ZZ_IO.h"

//#include "WebCamera.h"
//TODO 传感器采样率 流媒体 显示器 cpu设备表


#define SMARTCOMMUTITY "/home/pi/SmartCommunity"

using namespace std;

int fd;

zz_io zzio(0);

HumitureSensor  humitureSensor(0, 0, "humiture_sensor.db", "humiture", "humiture_sensor_config");
LaserSensor laserSensor(0, 1, "laser_sensor.db", "hasObstacles", "laser_sensor_config");
PM25Sensor pm25Sensor(0, 4, 10, "dust_sensor.db", "dust_snesor", "dust_sensor_config");
InfraredTemperatureSensor inTempSensor(0, "infrared_temperature_sensor.db", "temperature", "config");

PI_THREAD(HumitureSensorThread)
{
	piLock(0);

	cout << endl;
	cout << "======温湿度传感器======" << endl;
	cout << "数据库名称: " << humitureSensor.buffer.getDbName() << endl;
	cout << "数据表名称: " << humitureSensor.buffer.getDataTbaleName() << endl;
	cout << "配置表名称: " << humitureSensor.buffer.getConfigTableName() << endl << endl;
	cout << "数字引脚: " << humitureSensor.getPin() << endl << endl;

	cout << "读取温湿度传感器配置..." << endl;
	humitureSensor.setConfig(humitureSensor.readConfigFromBuffer());
	cout << "sample: " << humitureSensor.config.getSample() << endl;

	std::cout << "读取记录温湿度传感器数据..." << endl;
	piUnlock(0);
	while (true)
	{
		
		delay(humitureSensor.config.getSample() *1000);
		piLock(0);
		humitureSensor.readSensorData();
		humitureSensor.recordSensorData();
		piUnlock(0);
		//cout << "温度: " << humitureSensor->getTemperature() << "湿度: " << humitureSensor->getHumidity() << endl;
	}
}

PI_THREAD(LaserSensorThread)
{
	piLock(0);
	
	cout << endl;
	cout << "======激光传感器======" << endl;
	cout << "数据库名称: " << laserSensor.buffer.getDbName() << endl;
	cout << "数据表名称: " << laserSensor.buffer.getDataTbaleName() << endl;
	cout << "配置表名称: " << laserSensor.buffer.getConfigTableName() << endl << endl;
	cout << "模拟引脚: " << laserSensor.getPin() << endl << endl;

	cout << "读取激光传感器配置..." << endl;
	laserSensor.setConfig(laserSensor.readConfigFromBuffer());
	cout << "sample: " << laserSensor.config.getSample() << endl;
	piUnlock(0);

	while (true)
	{
		delay(laserSensor.config.getSample() * 1000);
		piLock(0);
		laserSensor.readSensorData();
		laserSensor.recordSensorData();
		piUnlock(0);
	}
}

PI_THREAD(PM25SensorThread)
{
	piLock(0);
	
	cout << endl;
	cout << "======PM2.5传感器======" << endl;
	cout << "数据库名称: " << pm25Sensor.buffer.getDbName() << endl;
	cout << "数据表名称: " << pm25Sensor.buffer.getDataTbaleName() << endl;
	cout << "配置表名称: " << pm25Sensor.buffer.getConfigTableName() << endl << endl;
	cout << "数字引脚: " << pm25Sensor.getPin() << endl;
	cout << "模拟引脚: " << pm25Sensor.getChannel() << endl << endl;

	cout << "读取PM2.5传感器配置..." << endl;
	pm25Sensor.setConfig(pm25Sensor.readConfigFromBuffer());
	cout << "sample: " << pm25Sensor.config.getSample() << endl;
	piUnlock(0);

	while (true)
	{
		int a;
		delay(pm25Sensor.config.getSample() * 1000);
		piLock(0);
		pm25Sensor.readSensorData();
		pm25Sensor.recordSensorData();
		//printf("%f", pm25Sensor.getPm25Density());
		piUnlock(0);
	}
}


PI_THREAD(ModuleThread)
{
	while (true)
	{
		zzio.getRelayStatus();
		zzio.updateBuffer();
		zzio.operate();
		delay(1000);
	}
}

PI_THREAD(PythonTread)
{
	
	piLock(0);
	

	PyRun_SimpleString("import sys");
	PyRun_SimpleString("sys.path.append('/home/pi/smartScript/')");


	//char szFile[] = "/home/pi/smartScript/SCript.py";
	//FILE* fp = fopen(szFile, "r");
	//if (!fp)
	//	cerr << "open faild\n";
	//PyRun_SimpleFile(fp, szFile);
	

	PyObject* pModule = PyImport_ImportModule("MLX90614");
	PyObject* pDict = PyModule_GetDict(pModule);
	PyObject* pClassMLX90614 = PyDict_GetItemString(pDict, "MLX90614");
	PyObject* pConstruct = PyInstanceMethod_New(pClassMLX90614);
	PyObject* pIns = PyObject_CallObject(pConstruct, nullptr);
	PyObject* pResAmbTemp =  PyObject_CallMethod(pIns, "get_amb_temp", nullptr);

	piUnlock(0);

	while (true)
	{
		PyObject* pResObjTemp = PyObject_CallMethod(pIns, "get_obj_temp", nullptr);
		double ampTemp = PyFloat_AsDouble(pResAmbTemp);
		double objTemp = PyFloat_AsDouble(pResObjTemp);
		inTempSensor.setTemp(ampTemp, objTemp);
		inTempSensor.recordSensorData();
		//cout << "环境温度: " << ambTemp << "物体温度: " << objTemp << endl;
		delay(1000);
	}

}


void roll()
{
	cout << "=====温湿度传感器=====\n";
	cout << "温度: " << humitureSensor.getTemperature() << "湿度: " << humitureSensor.getHumidity() << endl;
	cout << "=====激光传感器=====\n";
	if (laserSensor.hasObstacles())
	{
		cout << "有障碍物\n";
	}
	else
	{
		cout << "无障碍物\n";
	}
	cout << "=====PM2.5传感器=====\n";
	cout << "PM2.5: " << pm25Sensor.getPm25Density() << endl;
	cout << "=====红外测温仪器=====\n";
	double ampTemp, objTemp;
	inTempSensor.getTemp(&ampTemp, &objTemp);
	cout << "环境温度: " << ampTemp << "物体温度: " << objTemp << endl;

	cout << "=====开关量采集模块=====\n";

	if (zzio.getRelay0Status())
		cout << "开关1: open\n";
	else
		cout << "开关1: close\n";
	if (zzio.getRelay1Stauts())
		cout << "开关2: opend\n";
	else
		cout << "开关2: close\n";
	delay(1000);
}

void humiture_sensor()
{

	cout << endl;
	cout << "======温湿度传感器======" << endl;
	cout << "数据库名称: " << humitureSensor.buffer.getDbName() << endl;
	cout << "数据表名称: " << humitureSensor.buffer.getDataTbaleName() << endl;
	cout << "配置表名称: " << humitureSensor.buffer.getConfigTableName() << endl << endl;
	cout << "数字引脚: " << humitureSensor.getPin() << endl << endl;

	cout << "读取温湿度传感器配置..." << endl;
	humitureSensor.setConfig(humitureSensor.readConfigFromBuffer());
	cout << "sample: " << humitureSensor.config.getSample() << endl;

	std::cout << "读取记录温湿度传感器数据..." << endl;
	while (true)
	{

		delay(humitureSensor.config.getSample() * 1000);
		humitureSensor.readSensorData();
		humitureSensor.recordSensorData();

		cout << "温度: " << humitureSensor.getTemperature() << "湿度: " << humitureSensor.getHumidity() << endl;
	}
}

void laser_snesor()
{

	cout << endl;
	cout << "======激光传感器======" << endl;
	cout << "数据库名称: " << laserSensor.buffer.getDbName() << endl;
	cout << "数据表名称: " << laserSensor.buffer.getDataTbaleName() << endl;
	cout << "配置表名称: " << laserSensor.buffer.getConfigTableName() << endl << endl;
	cout << "模拟引脚: " << laserSensor.getPin() << endl << endl;

	cout << "读取激光传感器配置..." << endl;
	laserSensor.setConfig(laserSensor.readConfigFromBuffer());
	cout << "sample: " << laserSensor.config.getSample() << endl;

	while (true)
	{
		delay(laserSensor.config.getSample() * 1000);
		laserSensor.readSensorData();
		laserSensor.recordSensorData();
		cout << "has obstacles: " << laserSensor.hasObstacles() << endl;
	}
}

int main(void)
{
	Py_Initialize();
	//piThreadCreate(HumitureSensorThread);
	//piThreadCreate(LaserSensorThread);
	//piThreadCreate(PM25SensorThread);
	//piThreadCreate(PythonTread);

	thread laser(laser_snesor);
	thread humi(humiture_sensor);

	laser.join();
	humi.join();
	

	while (true)
	{
		roll();
		sleep(1);
	}

	return 0;
}

